package com.supermarket.shop.system.controller;

import com.supermarket.shop.system.domain.Commodity;
import com.supermarket.shop.system.service.ServiceCommodity;
import com.supermarket.shop.system.service.impl.CommodityServiceImpl;

import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

public class CommodityController {
    ServiceCommodity serviceCommodity = new CommodityServiceImpl();
    Scanner sc = new Scanner(System.in);

    public void start() throws IOException {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <商品> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加商品  2.删除商品  3.修改商品  4.查询商品  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加功能");
                    addCommodity();
                    break;
                case "2":
                    System.out.println("删除功能");
                    deleteCommodityByName();
                    break;
                case "3":
                    System.out.println("修改功能");
                    updateCommodityByname();
                    break;
                case "4":
                    System.out.println("查询功能");
                    findAllCommodity();
                    break;
                case "5":
                    System.out.println("感谢您使用商品管理系统, 再见!");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    private void updateCommodityByname() throws IOException {
        ///System.out.println("请输入商品名称:");
        String name=nameCommodity(true);
        if(name.equals("null")){
            return;
        }
        System.out.println("(如果不想修改名称输入0)");
        String nameupdate;
        if(sc.next().equals("0")){
            nameupdate=name;
        }else{
            System.out.println("请输入修改后商品名称:");
            nameupdate=nameCommodity(false);
        }
        if(name==null){
            System.out.println("退出:您可以选择其他操作!");
            return;
        }
        Commodity commodity=inputcommodity(name);
        serviceCommodity.updateCommodityByname(commodity,nameupdate);
    }

    //通过name查找商品名称来删除商品信息
    private void deleteCommodityByName() throws IOException {
        System.out.println("请输入商品名称:");
        String name=nameCommodity(true);
        if(name==null){
            System.out.println("退出:您可以选择其他操作!");
            return;
        }
        serviceCommodity.deleteCommodityByName(name);

    }

    //查找所有商品信息
    private void findAllCommodity() throws IOException {
        serviceCommodity.findAllCommodity();
    }

    //添加商品
    private void addCommodity() throws IOException {
        String name = nameCommodity(false);
        if(name==null){
            System.out.println("退出:您可以选择其他操作!");
            return;
        }
        Commodity commodity=inputcommodity(name);
        boolean bl=serviceCommodity.addCommodity(commodity);
        System.out.println("添加成功");
    }
    //输入name防止重名
    private String nameCommodity(boolean flag) throws IOException {
        Scanner sc = new Scanner(System.in);
        String name = null;
        while (true) {
            System.out.println("(如果你想退出去可以输入null)请输入商品名称:");
            name = sc.next();
            if(name.equals("null")){
                break;
            }
            boolean bl = serviceCommodity.isExist(name);
            if (!bl^flag) {
                break;
            }
            System.out.println("您输入的信息不正确,请您重新输入:");
        }
        return name;
    }

    //录入数据
    private Commodity inputcommodity(String name) throws IOException {
        Commodity commodity = new Commodity();
        commodity.setName(name);
        System.out.println("输入价格:");
        commodity.setPrice(sc.nextInt());
        System.out.println("输入库存:");
        commodity.setStock(sc.nextInt());
        System.out.println("输入生产企业名称:");
        commodity.setPop(sc.next());
        System.out.println("自动生成当前日期");
        commodity.setDate(new Date());
        System.out.println("自动生成商品序号");
        commodity.setId(serviceCommodity.maxCommodityId() + 1);
        return commodity;

    }


}
